package dev.ece.util.lib;

/**
 * 2维向量
 * @author 潜行的青衣
 * @see
 */
public class Vector2f extends Vector {

	public Vector2f() {
		super(new float[2]);
	}
	
	public Vector2f(Vector2f vector) {
		super(new float[] {vector.getX(), vector.getY()});
	}
	
	public Vector2f(float x, float y) {
		super(new float[] {x, y});
	}
	
	protected Vector2f(float[] vector) {
		super(vector);
	}
	
	public void set(float x, float y) {
		this.setX(x);
		this.setY(y);
	}
	
	public void set(Vector2f vector) {
		this.setX(vector.getX());
		this.setY(vector.getY());
	}
	
	public void splus(Vector2f start, Vector2f end) {
		this.set(end.getX() - start.getX(), end.getY() - start.getY());
	}
	
	public void setLength(float length) {
		if(length > 0) {
			this.normalize();
			this.set(this.getX() * length, this.getY() * length);
		}
	}
	
	public void add(float length) {
		if(length > 0) {
			float l = this.length();
			float x = this.getX();
			float y = this.getY();
			float rate = 1 + length / l;
			this.set(x * rate, y * rate); 
		}
	}
	
	public float length() {
		float x = this.getX();
		float y = this.getY();
		return (float) Math.sqrt(x * x + y * y);
	}
	
	public void normalize() {
		float l = this.length();
		if(l == 0) {
			throw new IllegalStateException("向量长度为0！");
		}
		this.setX(this.getX() / l);
		this.setY(this.getY() / l);
	}
	
	/**
	 * 插值运算
	 * (v2 - v1) * alpha + v1 = v1 * (1 - alpha) + v2 * alpha;
	 * @param v1
	 * @param v2
	 * @param alpha
	 */
	public final Vector2f interpolate(Vector2f v1, Vector2f v2, float alpha) {
		float leftAlpha = 1.0f - alpha;
		this.setX(leftAlpha * v1.getX() + alpha * v2.getX());
		this.setY(leftAlpha * v1.getY() + alpha * v2.getY());
		return this;
	}
	
	public void setX(float x) {
		this.getVector()[0] = x;
	}
	
	public float getX() {
		return getVector()[0];
	}
	
	public void setS(float s) {
		this.setX(s);
	}
	
	public float getS() {
		return this.getX();
	}
	
	public void setY(float y) {
		this.getVector()[1] = y;
	}
	
	public float getY() {
		return getVector()[1];
	}
	
	public void setT(float t) {
		this.setY(t);
	}
	
	public float getT() {
		return this.getY();
	}
	
}
